// 704. 二分查找

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
 var search = function (nums, target, index = 0) {
    if (nums.length === 1) {
        return nums[0] === target ? 0 : -1;
    }

    let length = nums.length;
    let result = null;
    let surplus = nums.slice(Math.ceil(length / 2), length);
    nums = nums.slice(0, Math.ceil(length / 2));
    console.log("momo", nums, surplus, index);
    result = nums.indexOf(target);

    if (result >= 0) {
        return result + index;
    }

    index = Math.ceil(length / 2) + index;

    if (surplus.length === 1 && result === -1) {
        if (surplus[0] === target) {
            return index;
        } else {
            return -1;
        }
    }

    return search(surplus, target, index);
};

console.log(search([-1, 0, 3, 5, 9, 12, 10], 9));
